package com.zhiyou.app.shop.website.dao.impl;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate4.HibernateCallback;
import org.springframework.stereotype.Component;

import com.zhiyou.app.shop.common.util.ShopConstants;
import com.zhiyou.app.shop.manager.dao.impl.BaseHibernateDAO;
import com.zhiyou.app.shop.website.dao.IUserBankDao;
import com.zhiyou.app.shop.website.dao.pojo.UserBank;

@Component
public class UserBankDaoImpl extends BaseHibernateDAO<UserBank, String> implements
		IUserBankDao {
	public UserBankDaoImpl(){
		super(UserBank.class);
	}

	public List<UserBank> findUserBankByAccount(final String userAccount) {
		List list = hibernateTemplate.execute(new HibernateCallback<List>() {
			public List doInHibernate(Session session)
					throws HibernateException {
				//String sql = "select b.* from user_bank b,user u where b.user_id =u.user_id and u.user_account= ?";
				String hql = "select distinct b from UserBank b,User u where b.userId = u.userAccount and u.userAccount = :userAccount";
				Query query = session.createQuery(hql);
				Map<String, Object> param = new HashMap<String, Object>();
				param.put("userAccount", userAccount);
				query.setProperties(param);
				return query.list();
			}
		});
		return list;
	}

	public List<UserBank> findAdminBankByAccount() {
		List list = hibernateTemplate.execute(new HibernateCallback<List>() {
			public List doInHibernate(Session session)
					throws HibernateException {
	
				String hql = "select b from UserBank b,User u where b.userId = u.userAccount and u.role = :role";
				Query query = session.createQuery(hql);
				Map<String, Object> param = new HashMap<String, Object>();
				param.put("role", ShopConstants.ADMIN);
				query.setProperties(param);
				return query.list();
			}
		});
		return list;
	}
	
}
